import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton
from PyQt5.QtCore import QTimer
import asyncio

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.layout = QVBoxLayout()
        self.button = QPushButton("运行协程")
        self.button.clicked.connect(self.run_coroutine)
        self.layout.addWidget(self.button)
        self.setLayout(self.layout)

    def run_coroutine(self):
        # 在子线程中运行 asyncio 任务
        asyncio.ensure_future(self.async_task())

    async def async_task(self):
        await asyncio.sleep(1)
        print("协程任务完成")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())